const g = require('../utils/global');

cc.Class({
    extends: cc.Component,

    properties: {
        // 本体
        plane: {
            type: cc.Sprite,
            default: null,
        },
        // 僚机
        wingPlane: {
            type: cc.Sprite,
            default: null,
        },
        // 大机翼
        wing: {
            type: cc.Sprite,
            default: null,
        },
        planList: {
            type: [cc.SpriteFrame],
            default: []
        },
        wingPlaneList: {
            type: [cc.SpriteFrame],
            default: []
        },
        wingList: {
            type: [cc.SpriteFrame],
            default: []
        },
    },

    setController(controller) {
        this.controller = controller;
    },

    // 初始化
    initWithModel(model) {
        this.model = model;
        this.node.x = model.startPos.x;
        this.node.y = model.startPos.y;
    },

    // 碰撞发生
    onCollisionEnter(other, self) {
        cc.log(`other = ${other.tag}, self = ${self.tag}, attack times = ${this.model.attackTimes}`);
        if (other.tag === g.CollisionTag.enemy) {
            if (self.tag === g.CollisionTag.planeWing && this.model.attackTimes !== 3) {
                // 机翼已经没有了
                return;
            }
            let x = this.node.x;
            let y = this.node.y;
            if(this.model.attackTimes === 3) {
                // 第一次攻击
                y += 37;
            } else if (this.model.attackTimes === 2) {
                // 第二次攻击
                y += 110;
            } else {
                y += 82;
            }
            this.controller.collisionPlaneWithEnemy(x, y);
        }
    },

    // TODO 选择飞机
    selectPlane(n) {
        cc.log(`choose plane index = ${n}`);
        this.model.type = n;
    },

    // 更新视图
    updateView() {
        if(!this.model) {
            return;
        }
        const allCmd = this.model.cmd;
        if (allCmd.length < 1) {
            return;
        }
        const actions = [];
        let curTime = 0;
        for (const cmd of allCmd) {
            if (cmd.playTime > curTime) {
                const delay = cc.delayTime(cmd.playTime - curTime);
                actions.push(delay);
            }
            if (cmd.action === g.ActionType.planeMoveTo) {
                const move = cc.moveTo(g.AnimTime.planeMove, cmd.pos);
                actions.push(move);
            } else if (cmd.action === g.ActionType.planeMoveBy) {
                const move = cc.moveBy(g.AnimTime.planeMove, cmd.pos);
                actions.push(move)
            } else if (cmd.action === g.ActionType.planeDelayMove) {
                const moveTouch = cc.callFunc(function (target, pos) {
                    target.x += pos.x;
                    target.y += pos.y;
                }, this.node, cmd.pos);
                actions.push(moveTouch);
            } else if (cmd.action === g.ActionType.toDie) {
                const toDie = cc.callFunc(function (target) {
                    target.destroy();
                }, this.node);
                actions.push(toDie);
            } else if (cmd.action === g.ActionType.planeAttacked) {
                cc.log(`under attack, attack times = ${this.model.attackTimes}`);
                const attacked = cc.callFunc(function (target, view) {
                    if (view.model.attackTimes === 2) {
                        // 第一次被攻击, 机翼掉落
                        view.wing.node.destroy();
                    } else {
                        // 第二次被攻击, 僚机掉落
                        view.wingPlane.node.destroy();
                    }
                }, this.node, this);
                actions.push(attacked);
            }
            // 上次播放动画需要的时间
            curTime = cmd.playTime+cmd.keepTime;
        }
        if(actions.length === 1) {
            this.node.runAction(actions[0]);
        } else {
            this.node.runAction(cc.sequence(...actions));
        }
    }

    // update (dt) {},
});
